home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / hity wydania / Ubuntu 9.10 PL / karmelkowy-koliberek-desktop-9.10-i386-PL.iso / casper / filesystem.squashfs / usr / lib / firefox-3.5.5 / components / nsSafebrowsingApplication.js < prev    next >
Text File  |  2009-11-09  |  26KB  |  746 lines

  1. const Cc = Components.classes;
  2. const Ci = Components.interfaces;
  3.  
  4. // This is copied from toolkit/components/content/js/lang.js.
  5. // It seems cleaner to copy this rather than #include from so far away.
  6. Function.prototype.inherits = function(parentCtor) {
  7.   var tempCtor = function(){};
  8.   tempCtor.prototype = parentCtor.prototype;
  9.   this.superClass_ = parentCtor.prototype;
  10.   this.prototype = new tempCtor();
  11. }  
  12.  
  13. //@line 36 "/build/buildd/firefox-3.5-3.5.5+nobinonly/build-tree/mozilla/browser/components/safebrowsing/content/application.js"
  14.  
  15. // We instantiate this variable when we create the application.
  16. var gDataProvider = null;
  17.  
  18. // An instance of our application is a PROT_Application object. It
  19. // basically just populates a few globals and instantiates wardens,
  20. // the listmanager, and the about:blocked error page.
  21.  
  22. /**
  23.  * An instance of our application. There should be exactly one of these.
  24.  * 
  25.  * Note: This object should instantiated only at profile-after-change
  26.  * or later because the listmanager and the cryptokeymanager need to
  27.  * read and write data files. Additionally, NSS isn't loaded until
  28.  * some time around then (Moz bug #321024).
  29.  *
  30.  * @constructor
  31.  */
  32. function PROT_Application() {
  33.   this.debugZone= "application";
  34.  
  35. //@line 83 "/build/buildd/firefox-3.5-3.5.5+nobinonly/build-tree/mozilla/browser/components/safebrowsing/content/application.js"
  36.   
  37.   // expose some classes
  38.   this.PROT_PhishingWarden = PROT_PhishingWarden;
  39.   this.PROT_MalwareWarden = PROT_MalwareWarden;
  40.  
  41.   // Load data provider pref values
  42.   gDataProvider = new PROT_DataProvider();
  43.  
  44.   // expose the object
  45.   this.wrappedJSObject = this;
  46. }
  47.  
  48. var gInitialized = false;
  49. PROT_Application.prototype.initialize = function() {
  50.   if (gInitialized)
  51.     return;
  52.   gInitialized = true;
  53.  
  54.   var obs = Cc["@mozilla.org/observer-service;1"].
  55.             getService(Ci.nsIObserverService);
  56.   obs.addObserver(this, "xpcom-shutdown", true);
  57.  
  58.   // XXX: move table names to a pref that we originally will download
  59.   // from the provider (need to workout protocol details)
  60.   this.malwareWarden = new PROT_MalwareWarden();
  61.   this.malwareWarden.registerBlackTable("goog-malware-shavar");
  62.   this.malwareWarden.maybeToggleUpdateChecking();
  63.  
  64.   this.phishWarden = new PROT_PhishingWarden();
  65.   this.phishWarden.registerBlackTable("goog-phish-shavar");
  66.   this.phishWarden.maybeToggleUpdateChecking();
  67. }
  68.  
  69. PROT_Application.prototype.observe = function(subject, topic, data) {
  70.   switch (topic) {
  71.     case "xpcom-shutdown":
  72.       this.malwareWarden.shutdown();
  73.       this.phishWarden.shutdown();
  74.       break;
  75.   }
  76. }
  77.  
  78. /**
  79.  * @param name String The type of url to get (either Phish or Error).
  80.  * @return String the report phishing URL (localized).
  81.  */
  82. PROT_Application.prototype.getReportURL = function(name) {
  83.   return gDataProvider["getReport" + name + "URL"]();
  84. }
  85.  
  86. /**
  87.  * about:blocked implementation
  88.  */
  89. PROT_Application.prototype.newChannel = function(uri) {
  90.   var ioService = Cc["@mozilla.org/network/io-service;1"]
  91.                  .getService(Ci.nsIIOService);
  92.   var secMan = Cc["@mozilla.org/scriptsecuritymanager;1"]
  93.               .getService(Ci.nsIScriptSecurityManager);
  94.  
  95.   var childURI = ioService.newURI("chrome://browser/content/safebrowsing/blockedSite.xhtml",
  96.                                   null, null);
  97.   var channel = ioService.newChannelFromURI(childURI);
  98.   channel.originalURI = uri;
  99.   
  100.   // Drop chrome privilege
  101.   var principal = secMan.getCodebasePrincipal(uri);
  102.   channel.owner = principal;
  103.  
  104.   return channel;
  105. }
  106.  
  107. PROT_Application.prototype.getURIFlags = function(uri) {
  108.   // We don't particularly *want* people linking to this from
  109.   // untrusted content, but given that bad sites can cause this page
  110.   // to appear (e.g. by having an iframe pointing to known malware),
  111.   // we should code as though this is explicitly possible.
  112.   return Ci.nsIAboutModule.ALLOW_SCRIPT |
  113.          Ci.nsIAboutModule.URI_SAFE_FOR_UNTRUSTED_CONTENT;
  114. }
  115.  
  116. PROT_Application.prototype.QueryInterface = function(iid) {
  117.   if (iid.equals(Ci.nsISupports) ||
  118.       iid.equals(Ci.nsISupportsWeakReference) ||
  119.       iid.equals(Ci.nsIObserver) ||
  120.       iid.equals(Ci.nsIAboutModule))
  121.     return this;
  122.  
  123.   Components.returnCode = Components.results.NS_ERROR_NO_INTERFACE;
  124.   return null;
  125. }
  126. //@line 37 "/build/buildd/firefox-3.5-3.5.5+nobinonly/build-tree/mozilla/browser/components/safebrowsing/content/globalstore.js"
  127.  
  128.  
  129. // A class that encapsulates data provider specific values.  The
  130. // root of the provider pref tree is browser.safebrowsing.provider.
  131. // followed by a number, followed by specific properties.  The properties
  132. // that a data provider can supply are:
  133. //
  134. // name: The name of the provider
  135. // lookupURL: The URL to send requests to in enhanced mode
  136. // keyURL: Before we send URLs in enhanced mode, we need to encrypt them
  137. // reportURL: When shown a warning bubble, we send back the user decision
  138. //            (get me out of here/ignore warning) to this URL (strip cookies
  139. //            first).  This is optional.
  140. // gethashURL: Url for requesting complete hashes from the provider.
  141. // reportGenericURL: HTML page for general user feedback
  142. // reportPhishURL: HTML page for notifying the provider of a new phishing page
  143. // reportErrorURL: HTML page for notifying the provider of a false positive
  144.  
  145. const kDataProviderIdPref = 'browser.safebrowsing.dataProvider';
  146. const kProviderBasePref = 'browser.safebrowsing.provider.';
  147.  
  148. //@line 59 "/build/buildd/firefox-3.5-3.5.5+nobinonly/build-tree/mozilla/browser/components/safebrowsing/content/globalstore.js"
  149. const MOZ_OFFICIAL_BUILD = true;
  150. //@line 63 "/build/buildd/firefox-3.5-3.5.5+nobinonly/build-tree/mozilla/browser/components/safebrowsing/content/globalstore.js"
  151.  
  152. const MOZ_PARAM_LOCALE = /\{moz:locale\}/g;
  153. const MOZ_PARAM_CLIENT = /\{moz:client\}/g;
  154. const MOZ_PARAM_BUILDID = /\{moz:buildid\}/g;
  155. const MOZ_PARAM_VERSION = /\{moz:version\}/g;
  156.  
  157. /**
  158.  * Information regarding the data provider.
  159.  */
  160. function PROT_DataProvider() {
  161.   this.prefs_ = new G_Preferences();
  162.  
  163.   this.loadDataProviderPrefs_();
  164.   
  165.   // Watch for changes in the data provider and update accordingly.
  166.   this.prefs_.addObserver(kDataProviderIdPref,
  167.                           BindToObject(this.loadDataProviderPrefs_, this));
  168.  
  169.   // Watch for when anti-phishing is toggled on or off.
  170.   this.prefs_.addObserver(kPhishWardenEnabledPref,
  171.                           BindToObject(this.loadDataProviderPrefs_, this));
  172. }
  173.  
  174. /**
  175.  * Populate all the provider variables.  We also call this when whenever
  176.  * the provider id changes.
  177.  */
  178. PROT_DataProvider.prototype.loadDataProviderPrefs_ = function() {
  179.   // Currently, there's no UI for changing local list provider so we
  180.   // hard code the value for provider 0.
  181.   this.updateURL_ = this.getUrlPref_(
  182.         'browser.safebrowsing.provider.0.updateURL');
  183.  
  184.   var id = this.prefs_.getPref(kDataProviderIdPref, null);
  185.  
  186.   // default to 0
  187.   if (null == id)
  188.     id = 0;
  189.   
  190.   var basePref = kProviderBasePref + id + '.';
  191.  
  192.   this.name_ = this.prefs_.getPref(basePref + "name", "");
  193.  
  194.   // Urls used to get data from a provider
  195.   this.lookupURL_ = this.getUrlPref_(basePref + "lookupURL");
  196.   this.keyURL_ = this.getUrlPref_(basePref + "keyURL");
  197.   this.reportURL_ = this.getUrlPref_(basePref + "reportURL");
  198.   this.gethashURL_ = this.getUrlPref_(basePref + "gethashURL");
  199.  
  200.   // Urls to HTML report pages
  201.   this.reportGenericURL_ = this.getUrlPref_(basePref + "reportGenericURL");
  202.   this.reportErrorURL_ = this.getUrlPref_(basePref + "reportErrorURL");
  203.   this.reportPhishURL_ = this.getUrlPref_(basePref + "reportPhishURL");
  204.   this.reportMalwareURL_ = this.getUrlPref_(basePref + "reportMalwareURL")
  205.   this.reportMalwareErrorURL_ = this.getUrlPref_(basePref + "reportMalwareErrorURL")
  206.  
  207.   // Propagate the changes to the list-manager.
  208.   this.updateListManager_();
  209. }
  210.  
  211. /**
  212.  * The list manager needs urls to operate.  It needs a url to know where the
  213.  * table updates are, and it needs a url for decrypting enchash style tables.
  214.  */
  215. PROT_DataProvider.prototype.updateListManager_ = function() {
  216.   var listManager = Cc["@mozilla.org/url-classifier/listmanager;1"]
  217.                       .getService(Ci.nsIUrlListManager);
  218.  
  219.   // If we add support for changing local data providers, we need to add a
  220.   // pref observer that sets the update url accordingly.
  221.   listManager.setUpdateUrl(this.getUpdateURL());
  222.  
  223.   // setKeyUrl has the side effect of fetching a key from the server.
  224.   // This shouldn't happen if anti-phishing/anti-malware is disabled.
  225.   var isEnabled = this.prefs_.getPref(kPhishWardenEnabledPref, false) ||
  226.                   this.prefs_.getPref(kMalwareWardenEnabledPref, false);
  227.   if (isEnabled) {
  228.     listManager.setKeyUrl(this.keyURL_);
  229.   }
  230.  
  231.   listManager.setGethashUrl(this.getGethashURL());
  232. }
  233.  
  234. /**
  235.  * Lookup the value of a URL from prefs file and do parameter substitution.
  236.  */
  237. PROT_DataProvider.prototype.getUrlPref_ = function(prefName) {
  238.   var url = this.prefs_.getPref(prefName);
  239.  
  240.   var appInfo = Components.classes["@mozilla.org/xre/app-info;1"]
  241.                           .getService(Components.interfaces.nsIXULAppInfo);
  242.  
  243.   var mozClientStr = this.prefs_.getPref("browser.safebrowsing.clientid",
  244.                                          MOZ_OFFICIAL_BUILD ? 'navclient-auto-ffox' : appInfo.name);
  245.  
  246.   var versionStr = this.prefs_.getPref("browser.safebrowsing.clientver",
  247.                                        appInfo.version);
  248.  
  249.   // Parameter substitution
  250.   url = url.replace(MOZ_PARAM_LOCALE, this.getLocale_());
  251.   url = url.replace(MOZ_PARAM_CLIENT, mozClientStr);
  252.   url = url.replace(MOZ_PARAM_BUILDID, appInfo.appBuildID);
  253.   url = url.replace(MOZ_PARAM_VERSION, versionStr);
  254.   return url;
  255. }
  256.  
  257. /**
  258.  * @return String the browser locale (similar code is in nsSearchService.js)
  259.  */
  260. PROT_DataProvider.prototype.getLocale_ = function() {
  261.   const localePref = "general.useragent.locale";
  262.   var locale = this.getLocalizedPref_(localePref);
  263.   if (locale)
  264.     return locale;
  265.  
  266.   // Not localized
  267.   var prefs = new G_Preferences();
  268.   return prefs.getPref(localePref, "");
  269. }
  270.  
  271. /**
  272.  * @return String name of the localized pref, null if none exists.
  273.  */
  274. PROT_DataProvider.prototype.getLocalizedPref_ = function(aPrefName) {
  275.   // G_Preferences doesn't know about complex values, so we use the
  276.   // xpcom object directly.
  277.   var prefs = Cc["@mozilla.org/preferences-service;1"]
  278.               .getService(Ci.nsIPrefBranch);
  279.   try {
  280.     return prefs.getComplexValue(aPrefName, Ci.nsIPrefLocalizedString).data;
  281.   } catch (ex) {
  282.   }
  283.   return "";
  284. }
  285.  
  286. //////////////////////////////////////////////////////////////////////////////
  287. // Getters for the remote provider pref values mentioned above.
  288. PROT_DataProvider.prototype.getName = function() {
  289.   return this.name_;
  290. }
  291.  
  292. PROT_DataProvider.prototype.getUpdateURL = function() {
  293.   return this.updateURL_;
  294. }
  295.  
  296. PROT_DataProvider.prototype.getLookupURL = function() {
  297.   return this.lookupURL_;
  298. }
  299.  
  300. PROT_DataProvider.prototype.getGethashURL = function() {
  301.   return this.gethashURL_;
  302. }
  303.  
  304. PROT_DataProvider.prototype.getReportGenericURL = function() {
  305.   return this.reportGenericURL_;
  306. }
  307. PROT_DataProvider.prototype.getReportErrorURL = function() {
  308.   return this.reportErrorURL_;
  309. }
  310. PROT_DataProvider.prototype.getReportPhishURL = function() {
  311.   return this.reportPhishURL_;
  312. }
  313. PROT_DataProvider.prototype.getReportMalwareURL = function() {
  314.   return this.reportMalwareURL_;
  315. }
  316. PROT_DataProvider.prototype.getReportMalwareErrorURL = function() {
  317.   return this.reportMalwareErrorURL_;
  318. }
  319. //@line 37 "/build/buildd/firefox-3.5-3.5.5+nobinonly/build-tree/mozilla/browser/components/safebrowsing/content/list-warden.js"
  320.  
  321. // A warden that knows how to register lists with a listmanager and keep them
  322. // updated if necessary.  The ListWarden also provides a simple interface to
  323. // check if a URL is evil or not.  Specialized wardens like the PhishingWarden
  324. // inherit from it.
  325. //
  326. // Classes that inherit from ListWarden are responsible for calling
  327. // enableTableUpdates or disableTableUpdates.  This usually entails
  328. // registering prefObservers and calling enable or disable in the base
  329. // class as appropriate.
  330. //
  331.  
  332. /**
  333.  * Abtracts the checking of user/browser actions for signs of
  334.  * phishing. 
  335.  *
  336.  * @constructor
  337.  */
  338. function PROT_ListWarden() {
  339.   this.debugZone = "listwarden";
  340.   var listManager = Cc["@mozilla.org/url-classifier/listmanager;1"]
  341.                       .getService(Ci.nsIUrlListManager);
  342.   this.listManager_ = listManager;
  343.  
  344.   // Once we register tables, their respective names will be listed here.
  345.   this.blackTables_ = [];
  346.   this.whiteTables_ = [];
  347. }
  348.  
  349. PROT_ListWarden.IN_BLACKLIST = 0
  350. PROT_ListWarden.IN_WHITELIST = 1
  351. PROT_ListWarden.NOT_FOUND = 2
  352.  
  353. /**
  354.  * Tell the ListManger to keep all of our tables updated
  355.  */
  356.  
  357. PROT_ListWarden.prototype.enableBlacklistTableUpdates = function() {
  358.   for (var i = 0; i < this.blackTables_.length; ++i) {
  359.     this.listManager_.enableUpdate(this.blackTables_[i]);
  360.   }
  361. }
  362.  
  363. /**
  364.  * Tell the ListManager to stop updating our tables
  365.  */
  366.  
  367. PROT_ListWarden.prototype.disableBlacklistTableUpdates = function() {
  368.   for (var i = 0; i < this.blackTables_.length; ++i) {
  369.     this.listManager_.disableUpdate(this.blackTables_[i]);
  370.   }
  371. }
  372.  
  373. /**
  374.  * Tell the ListManager to update whitelist tables.  They may be enabled even
  375.  * when other updates aren't, for performance reasons.
  376.  */
  377. PROT_ListWarden.prototype.enableWhitelistTableUpdates = function() {
  378.   for (var i = 0; i < this.whiteTables_.length; ++i) {
  379.     this.listManager_.enableUpdate(this.whiteTables_[i]);
  380.   }
  381. }
  382.  
  383. /**
  384.  * Tell the ListManager to stop updating whitelist tables.
  385.  */
  386. PROT_ListWarden.prototype.disableWhitelistTableUpdates = function() {
  387.   for (var i = 0; i < this.whiteTables_.length; ++i) {
  388.     this.listManager_.disableUpdate(this.whiteTables_[i]);
  389.   }
  390. }
  391.  
  392. /**
  393.  * Register a new black list table with the list manager
  394.  * @param tableName - name of the table to register
  395.  * @returns true if the table could be registered, false otherwise
  396.  */
  397.  
  398. PROT_ListWarden.prototype.registerBlackTable = function(tableName) {
  399.   var result = this.listManager_.registerTable(tableName, false);
  400.   if (result) {
  401.     this.blackTables_.push(tableName);
  402.   }
  403.   return result;
  404. }
  405.  
  406. /**
  407.  * Register a new white list table with the list manager
  408.  * @param tableName - name of the table to register
  409.  * @returns true if the table could be registered, false otherwise
  410.  */
  411.  
  412. PROT_ListWarden.prototype.registerWhiteTable = function(tableName) {
  413.   var result = this.listManager_.registerTable(tableName, false);
  414.   if (result) {
  415.     this.whiteTables_.push(tableName);
  416.   }
  417.   return result;
  418. }
  419. //@line 36 "/build/buildd/firefox-3.5-3.5.5+nobinonly/build-tree/mozilla/browser/components/safebrowsing/content/phishing-warden.js"
  420.  
  421.  
  422. // The warden checks request to see if they are for phishy pages. It
  423. // does so by querying our locally stored blacklists.
  424. // 
  425. // When the warden notices a problem, it queries all browser views
  426. // (each of which corresopnds to an open browser window) to see
  427. // whether one of them can handle it. A browser view can handle a
  428. // problem if its browser window has an HTMLDocument loaded with the
  429. // given URL and that Document hasn't already been flagged as a
  430. // problem. For every problematic URL we notice loading, at most one
  431. // Document is flagged as problematic. Otherwise you can get into
  432. // trouble if multiple concurrent phishy pages load with the same URL.
  433. //
  434. // Since we check URLs very early in the request cycle (in a progress
  435. // listener), the URL might not yet be associated with a Document when
  436. // we determine that it is phishy. So the the warden retries finding
  437. // a browser view to handle the problem until one can, or until it
  438. // determines it should give up (see complicated logic below).
  439. //
  440. // The warden has displayers that the browser view uses to render
  441. // different kinds of warnings (e.g., one that's shown before a page
  442. // loads as opposed to one that's shown after the page has already
  443. // loaded).
  444. //
  445. // Note: There is a single warden for the whole application.
  446. //
  447. // TODO better way to expose displayers/views to browser view
  448.  
  449. const kPhishWardenEnabledPref = "browser.safebrowsing.enabled";
  450.  
  451. /**
  452.  * Abtracts the checking of user/browser actions for signs of
  453.  * phishing. 
  454.  *
  455.  * @param progressListener nsIDocNavStartProgressListener
  456.  * @param tabbrowser XUL tabbrowser element
  457.  * @constructor
  458.  */
  459. function PROT_PhishingWarden() {
  460.   PROT_ListWarden.call(this);
  461.  
  462.   this.debugZone = "phishwarden";
  463.  
  464.   // Use this to query preferences
  465.   this.prefs_ = new G_Preferences();
  466.  
  467.   // We need to know whether we're enabled and whether we're in advanced
  468.   // mode, so reflect the appropriate preferences into our state.
  469.  
  470.   // Global preference to enable the phishing warden
  471.   this.phishWardenEnabled_ = this.prefs_.getPref(kPhishWardenEnabledPref, null);
  472.  
  473.   // Get notifications when the phishing warden enabled pref changes
  474.   var phishWardenPrefObserver = 
  475.     BindToObject(this.onPhishWardenEnabledPrefChanged, this);
  476.   this.prefs_.addObserver(kPhishWardenEnabledPref, phishWardenPrefObserver);
  477.  
  478.   G_Debug(this, "phishWarden initialized");
  479. }
  480.  
  481. PROT_PhishingWarden.inherits(PROT_ListWarden);
  482.  
  483. PROT_PhishingWarden.prototype.QueryInterface = function(iid) {
  484.   if (iid.equals(Ci.nsISupports) || 
  485.       iid.equals(Ci.nsISupportsWeakReference))
  486.     return this;
  487.   throw Components.results.NS_ERROR_NO_INTERFACE;
  488. }
  489.  
  490. /**
  491.  * Cleanup on shutdown.
  492.  */
  493. PROT_PhishingWarden.prototype.shutdown = function() {
  494.   this.prefs_.removeAllObservers();
  495.   this.listManager_ = null;
  496. }
  497.  
  498. /**
  499.  * When a preference (either advanced features or the phishwarden
  500.  * enabled) changes, we might have to start or stop asking for updates. 
  501.  * 
  502.  * This is a little tricky; we start or stop management only when we
  503.  * have complete information we can use to determine whether we
  504.  * should.  It could be the case that one pref or the other isn't set
  505.  * yet (e.g., they haven't opted in/out of advanced features). So do
  506.  * nothing unless we have both pref values -- we get notifications for
  507.  * both, so eventually we will start correctly.
  508.  */ 
  509. PROT_PhishingWarden.prototype.maybeToggleUpdateChecking = function() {
  510.   var phishWardenEnabled = this.prefs_.getPref(kPhishWardenEnabledPref, null);
  511.  
  512.   G_Debug(this, "Maybe toggling update checking. " +
  513.           "Warden enabled? " + phishWardenEnabled);
  514.  
  515.   // Do nothing unless both prefs are set.  They can be null (unset), true, or
  516.   // false.
  517.   if (phishWardenEnabled === null)
  518.     return;
  519.  
  520.   // We update and save to disk all tables
  521.   if (phishWardenEnabled === true) {
  522.     this.enableBlacklistTableUpdates();
  523.     this.enableWhitelistTableUpdates();
  524.   } else {
  525.     // Anti-phishing is off, disable table updates
  526.     this.disableBlacklistTableUpdates();
  527.     this.disableWhitelistTableUpdates();
  528.   }
  529. }
  530.  
  531. /**
  532.  * Deal with a user changing the pref that says whether we should 
  533.  * enable the phishing warden (i.e., that SafeBrowsing is active)
  534.  *
  535.  * @param prefName Name of the pref holding the value indicating whether
  536.  *                 we should enable the phishing warden
  537.  */
  538. PROT_PhishingWarden.prototype.onPhishWardenEnabledPrefChanged = function(
  539.                                                                     prefName) {
  540.   // Just to be safe, ignore changes to sub prefs.
  541.   if (prefName != "browser.safebrowsing.enabled")
  542.     return;
  543.  
  544.   this.phishWardenEnabled_ = 
  545.     this.prefs_.getPref(prefName, this.phishWardenEnabled_);
  546.   this.maybeToggleUpdateChecking();
  547. }
  548. //@line 37 "/build/buildd/firefox-3.5-3.5.5+nobinonly/build-tree/mozilla/browser/components/safebrowsing/content/malware-warden.js"
  549.  
  550. // This warden manages updates to the malware list
  551.  
  552. const kMalwareWardenEnabledPref = "browser.safebrowsing.malware.enabled";
  553.  
  554. function PROT_MalwareWarden() {
  555.   PROT_ListWarden.call(this);
  556.  
  557.   this.debugZone = "malwarewarden";
  558.  
  559.   // Use this to query preferences
  560.   this.prefs_ = new G_Preferences();
  561.  
  562.   // Global preference to enable the malware warden
  563.   this.malwareWardenEnabled_ =
  564.     this.prefs_.getPref(kMalwareWardenEnabledPref, null);
  565.  
  566.   // Get notifications when the malware warden enabled pref changes
  567.   var malwareWardenPrefObserver =
  568.     BindToObject(this.onMalwareWardenEnabledPrefChanged, this);
  569.   this.prefs_.addObserver(kMalwareWardenEnabledPref, malwareWardenPrefObserver);
  570.  
  571.   // Add a test chunk to the database
  572.   var testData = "mozilla.com/firefox/its-an-attack.html";
  573.  
  574.   var testUpdate =
  575.     "n:1000\ni:test-malware-simple\nad:1\n" +
  576.     "a:1:32:" + testData.length + "\n" +
  577.     testData;
  578.     
  579.   testData = "mozilla.com/firefox/its-a-trap.html";
  580.   testUpdate +=
  581.     "n:1000\ni:test-phish-simple\nad:1\n" +
  582.     "a:1:32:" + testData.length + "\n" +
  583.     testData;
  584.  
  585.   var dbService_ = Cc["@mozilla.org/url-classifier/dbservice;1"]
  586.                    .getService(Ci.nsIUrlClassifierDBService);
  587.  
  588.   var listener = {
  589.     QueryInterface: function(iid)
  590.     {
  591.       if (iid.equals(Ci.nsISupports) ||
  592.           iid.equals(Ci.nsIUrlClassifierUpdateObserver))
  593.         return this;
  594.       throw Cr.NS_ERROR_NO_INTERFACE;
  595.     },
  596.  
  597.     updateUrlRequested: function(url) { },
  598.     streamFinished: function(status) { },
  599.     updateError: function(errorCode) { },
  600.     updateSuccess: function(requestedTimeout) { }
  601.   };
  602.  
  603.   try {
  604.     dbService_.beginUpdate(listener,
  605.                            "test-malware-simple,test-phish-simple", "");
  606.     dbService_.beginStream("", "");
  607.     dbService_.updateStream(testUpdate);
  608.     dbService_.finishStream();
  609.     dbService_.finishUpdate();
  610.   } catch(ex) {
  611.     // beginUpdate will throw harmlessly if there's an existing update
  612.     // in progress, ignore failures.
  613.   }
  614.   G_Debug(this, "malwareWarden initialized");
  615. }
  616.  
  617. PROT_MalwareWarden.inherits(PROT_ListWarden);
  618.  
  619. /**
  620.  * Cleanup on shutdown.
  621.  */
  622. PROT_MalwareWarden.prototype.shutdown = function() {
  623.   this.prefs_.removeAllObservers();
  624.  
  625.   this.listManager_ = null;
  626. }
  627.  
  628. /**
  629.  * When a preference changes, we might have to start or stop asking for
  630.  * updates.
  631.  */
  632. PROT_MalwareWarden.prototype.maybeToggleUpdateChecking = function() {
  633.   var malwareWardenEnabled = this.prefs_.getPref(kMalwareWardenEnabledPref,
  634.                                                  null);
  635.  
  636.   G_Debug(this, "Maybe toggling update checking. " +
  637.           "Warden enabled? " + malwareWardenEnabled);
  638.  
  639.   // Do nothing unless thre pref is set
  640.   if (malwareWardenEnabled === null)
  641.     return;
  642.  
  643.   // We update and save to disk all tables
  644.   if (malwareWardenEnabled === true) {
  645.     this.enableBlacklistTableUpdates();
  646.   } else {
  647.     // Anti-malware is off, disable table updates
  648.     this.disableBlacklistTableUpdates();
  649.   }
  650. }
  651.  
  652. /**
  653.  * Deal with a user changing the pref that says whether we should 
  654.  * enable the malware warden.
  655.  *
  656.  * @param prefName Name of the pref holding the value indicating whether
  657.  *                 we should enable the malware warden
  658.  */
  659. PROT_MalwareWarden.prototype.onMalwareWardenEnabledPrefChanged = function(
  660.                                                                     prefName) {
  661.   // Just to be safe, ignore changes to sub prefs.
  662.   if (prefName != kMalwareWardenEnabledPref)
  663.     return;
  664.  
  665.   this.malwareWardenEnabled_ =
  666.     this.prefs_.getPref(prefName, this.malwareWardenEnabled_);
  667.   this.maybeToggleUpdateChecking();
  668. }
  669. //@line 18 "/build/buildd/firefox-3.5-3.5.5+nobinonly/build-tree/mozilla/browser/components/safebrowsing/src/nsSafebrowsingApplication.js"
  670.  
  671. var modScope = this;
  672. function Init() {
  673.   var jslib = Cc["@mozilla.org/url-classifier/jslib;1"]
  674.               .getService().wrappedJSObject;
  675.   modScope.G_Debug = jslib.G_Debug;
  676.   modScope.G_Assert = jslib.G_Assert;
  677.   modScope.G_Alarm = jslib.G_Alarm;
  678.   modScope.G_ConditionalAlarm = jslib.G_ConditionalAlarm;
  679.   modScope.G_ObserverWrapper = jslib.G_ObserverWrapper;
  680.   modScope.G_Preferences = jslib.G_Preferences;
  681.   modScope.PROT_XMLFetcher = jslib.PROT_XMLFetcher;
  682.   modScope.BindToObject = jslib.BindToObject;
  683.   modScope.G_Protocol4Parser = jslib.G_Protocol4Parser;
  684.   modScope.PROT_UrlCrypto = jslib.PROT_UrlCrypto;
  685.   modScope.RequestBackoff = jslib.RequestBackoff;
  686.   
  687.   // We only need to call Init once
  688.   modScope.Init = function() {};
  689. }
  690.  
  691. // Module object
  692. function SafebrowsingApplicationMod() {
  693.   this.firstTime = true;
  694.   this.cid = Components.ID("{c64d0bcb-8270-4ca7-a0b3-3380c8ffecb5}");
  695.   this.progid = "@mozilla.org/safebrowsing/application;1";
  696. }
  697.  
  698. SafebrowsingApplicationMod.prototype.registerSelf = function(compMgr, fileSpec, loc, type) {
  699.   if (this.firstTime) {
  700.     this.firstTime = false;
  701.     throw Components.results.NS_ERROR_FACTORY_REGISTER_AGAIN;
  702.   }
  703.   compMgr = compMgr.QueryInterface(Ci.nsIComponentRegistrar);
  704.   compMgr.registerFactoryLocation(this.cid,
  705.                                   "Safebrowsing Application Module",
  706.                                   this.progid,
  707.                                   fileSpec,
  708.                                   loc,
  709.                                   type);
  710.   
  711.   compMgr.registerFactoryLocation(this.cid,
  712.                                   "UrlClassifier Blocked Error Page",
  713.                                   "@mozilla.org/network/protocol/about;1?what=blocked",
  714.                                   fileSpec,
  715.                                   loc,
  716.                                   type);
  717. };
  718.  
  719. SafebrowsingApplicationMod.prototype.getClassObject = function(compMgr, cid, iid) {  
  720.   if (!cid.equals(this.cid))
  721.     throw Components.results.NS_ERROR_NO_INTERFACE;
  722.   if (!iid.equals(Ci.nsIFactory))
  723.     throw Components.results.NS_ERROR_NOT_IMPLEMENTED;
  724.  
  725.   return this.factory;
  726. }
  727.  
  728. SafebrowsingApplicationMod.prototype.canUnload = function(compMgr) {
  729.   return true;
  730. }
  731.  
  732. SafebrowsingApplicationMod.prototype.factory = {
  733.   createInstance: function(outer, iid) {
  734.     if (outer != null)
  735.       throw Components.results.NS_ERROR_NO_AGGREGATION;
  736.     Init();
  737.     return new PROT_Application();
  738.   }
  739. };
  740.  
  741. var ApplicationModInst = new SafebrowsingApplicationMod();
  742.  
  743. function NSGetModule(compMgr, fileSpec) {
  744.   return ApplicationModInst;
  745. }
  746.